
//
// 版权所有 (c) 2021-2023 JR
//
// 特此免费授予获得本软件的任何人以处理本软件的权利，但须遵守以下条件：在所有副本或重要部分的软件中必须包括上述版权声明和本许可声明。
//
// 软件按“原样”提供，不提供任何形式的明示或暗示的保证，包括但不限于对适销性、适用性和非侵权的保证。
// 在任何情况下，作者或版权持有人均不对任何索赔、损害或其他责任负责，无论是因合同、侵权或其他方式引起的，与软件或其使用或其他交易有关。

using AngleSharp.Dom;
using RazorEngine.Compilation.ImpromptuInterface.Optimization;
using System.Linq.Dynamic.Core.CustomTypeProviders;
using System.Reflection;

namespace EMC.VISITOR.Core;

public static class SqlSugarSetup
{
    /// <summary>
    /// SqlSugar 上下文初始化
    /// </summary>
    /// <param name="services"></param>
    public static void AddSqlSugar(this IServiceCollection services)
    {
        // 自定义 SqlSugar 雪花ID算法
        SnowFlakeSingle.WorkId = App.GetOptions<SnowIdOptions>().WorkerId;
        StaticConfig.CustomSnowFlakeFunc = () =>
        {
            return YitIdHelper.NextId();
        };
        //程序启动时配置   - 配置了此项目
        StaticConfig.DynamicExpressionParserType = typeof(DynamicExpressionParser);
        StaticConfig.DynamicExpressionParsingConfig = new ParsingConfig()//用到SqlFunc需要配置这个属性
        {
            CustomTypeProvider = new SqlSugarTypeProvider()//这个类需要自已定义下面有
        };
        //使用SqlFunc.Exists就需要用到上面你的配置
        //var list3 = db.Queryable<UnitPerson011>().Where("it", $"SqlFunc.Exists(it.Address.Id)").ToList();

        Console.WriteLine("测试读取配置数据之机器码：" + SnowFlakeSingle.WorkId);
        var dbOptions = App.GetOptions<DbConnectionOptions>();
        dbOptions.ConnectionConfigs.ForEach(SetDbConfig);

        SqlSugarScope sqlSugar = new(dbOptions.ConnectionConfigs.Adapt<List<ConnectionConfig>>(), db =>
        {
            dbOptions.ConnectionConfigs.ForEach(config =>
            {
                var dbProvider = db.GetConnectionScope(config.ConfigId);
                SetDbAop(dbProvider, dbOptions.EnableConsoleSql);
                SetDbDiffLog(dbProvider, db, config);
            });
        });

        services.AddSingleton<ISqlSugarClient>(sqlSugar); // 单例注册
        services.AddScoped(typeof(SqlSugarRepository<>)); // 仓储注册
        services.AddUnitOfWork<SqlSugarUnitOfWork>(); // 事务与工作单元注册

        // 初始化数据库表结构及种子数据
        dbOptions.ConnectionConfigs.ForEach(config =>
        {
            InitDatabase(sqlSugar, config);
        });


    }

    /// <summary>
    /// 配置连接属性
    /// </summary>
    /// <param name="config"></param>
    public static void SetDbConfig(DbConnectionConfig config)
    {
        var configureExternalServices = new ConfigureExternalServices
        {
            EntityNameService = (type, entity) => // 处理表
            {
                entity.IsDisabledDelete = true; // 禁止删除非 sqlsugar 创建的列
                // 只处理贴了特性[SugarTable]表
                if (!type.GetCustomAttributes<SugarTable>().Any())
                    return;
                if (config.DbSettings.EnableUnderLine && !entity.DbTableName.Contains('_'))
                    entity.DbTableName = UtilMethods.ToUnderLine(entity.DbTableName); // 驼峰转下划线


                entity.DbTableName = config.TableSettings?.TablePrefix + entity.DbTableName + config.TableSettings?.TableSuffix;
            },
            EntityService = (type, column) => // 处理列
            {
                // 只处理贴了特性[SugarColumn]列
                if (!type.GetCustomAttributes<SugarColumn>().Any())
                    return;
                if (new NullabilityInfoContext().Create(type).WriteState is NullabilityState.Nullable)
                    column.IsNullable = true;
                if (config.DbSettings.EnableUnderLine && !column.IsIgnore && !column.DbColumnName.Contains('_'))
                    column.DbColumnName = UtilMethods.ToUnderLine(column.DbColumnName); // 驼峰转下划线

                if (config.DbType == SqlSugar.DbType.Oracle)
                {
                    if (type.PropertyType == typeof(long) || type.PropertyType == typeof(long?))
                        column.DataType = "number(18)";
                    if (type.PropertyType == typeof(bool) || type.PropertyType == typeof(bool?))
                        column.DataType = "number(1)";
                    if (type.PropertyType == typeof(string))
                    {
                        if (column.Length <= 0)
                            column.DataType = "nvarchar2(255)";
                        else
                            column.DataType = "nvarchar2"; //会自动加上 nvarchar2(column.Length)
                    }

                }
            },
            DataInfoCacheService = new SqlSugarCache(),
        };
        config.ConfigureExternalServices = configureExternalServices;
        config.InitKeyType = InitKeyType.Attribute;
        config.IsAutoCloseConnection = true;

        //config.IndexSuffix
        //pgsql没用，试过，失败了
        //if (config.DbType == SqlSugar.DbType.PostgreSQL)
        //{
        //    //此种方案只能跨Scheme查询(跨Scheme就是在同一库下)
        //    config.DbLinkName = config.DbName;
        //    config.ConnectionString = $"{config.ConnectionString}SearchPath={config.DbLinkName}";
        //}
        config.MoreSettings = new ConnMoreSettings
        {
            IsAutoRemoveDataCache = true,
            IsAutoDeleteQueryFilter = true, // 启用删除查询过滤器
            IsAutoUpdateQueryFilter = true, // 启用更新查询过滤器
            SqlServerCodeFirstNvarchar = true // 采用Nvarchar
        };
    }

    /// <summary>
    /// 配置Aop
    /// </summary>
    /// <param name="db"></param>
    /// <param name="enableConsoleSql"></param>
    public static void SetDbAop(SqlSugarScopeProvider db, bool enableConsoleSql)
    {
        var config = db.CurrentConnectionConfig;

        // 设置超时时间
        db.Ado.CommandTimeOut = 30;

        // 打印SQL语句
        if (enableConsoleSql)
        {
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                var originColor = Console.ForegroundColor;
                if (sql.StartsWith("SELECT", StringComparison.OrdinalIgnoreCase))
                    Console.ForegroundColor = ConsoleColor.Green;
                if (sql.StartsWith("UPDATE", StringComparison.OrdinalIgnoreCase) || sql.StartsWith("INSERT", StringComparison.OrdinalIgnoreCase))
                    Console.ForegroundColor = ConsoleColor.Yellow;
                if (sql.StartsWith("DELETE", StringComparison.OrdinalIgnoreCase))
                    Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("【" + DateTime.Now + "——执行SQL】\r\n" + UtilMethods.GetSqlString(config.DbType, sql, pars) + "\r\n");
                Console.ForegroundColor = originColor;
                App.PrintToMiniProfiler("SqlSugar", "Info", sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
            };
            db.Aop.OnError = ex =>
            {
                if (ex.Parametres == null) return;
                var originColor = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.DarkRed;
                var pars = db.Utilities.SerializeObject(((SugarParameter[])ex.Parametres).ToDictionary(it => it.ParameterName, it => it.Value));
                Console.WriteLine("【" + DateTime.Now + "——错误SQL】\r\n" + UtilMethods.GetSqlString(config.DbType, ex.Sql, (SugarParameter[])ex.Parametres) + "\r\n");
                Console.ForegroundColor = originColor;
                App.PrintToMiniProfiler("SqlSugar", "Error", $"{ex.Message}{Environment.NewLine}{ex.Sql}{pars}{Environment.NewLine}");
            };
            db.Aop.OnLogExecuted = (sql, pars) =>
            {
                // 执行时间超过5秒
                if (db.Ado.SqlExecutionTime.TotalSeconds > 5)
                {
                    var fileName = db.Ado.SqlStackTrace.FirstFileName; // 文件名
                    var fileLine = db.Ado.SqlStackTrace.FirstLine; // 行号
                    var firstMethodName = db.Ado.SqlStackTrace.FirstMethodName; // 方法名
                    var log = $"【所在文件名】：{fileName}\r\n【代码行数】：{fileLine}\r\n【方法名】：{firstMethodName}\r\n" + $"【sql语句】：{UtilMethods.GetSqlString(config.DbType, sql, pars)}";
                    var originColor = Console.ForegroundColor;
                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                    Console.WriteLine(log);
                    Console.ForegroundColor = originColor;
                }
            };
        }
        // 数据审计
        db.Aop.DataExecuting = (oldValue, entityInfo) =>
        {
            //// 演示环境判断
            //if (entityInfo.EntityColumnInfo.IsPrimarykey)
            //{
            //    if (entityInfo.EntityName != nameof(SysJobDetail) && entityInfo.EntityName != nameof(SysJobTrigger) &&
            //        entityInfo.EntityName != nameof(SysLogOp) && entityInfo.EntityName != nameof(SysLogVis) &&
            //        entityInfo.EntityName != nameof(SysOnlineUser))
            //    {
            //        var isDemoEnv = App.GetService<SysConfigService>().GetConfigValue<bool>(CommonConst.SysDemoEnv).GetAwaiter().GetResult();
            //        if (isDemoEnv)
            //            throw Oops.Oh(ErrorCodeEnum.D1200);
            //    }
            //}

            if (entityInfo.OperationType == DataFilterType.InsertByObject)
            {
                // 主键(long类型)且没有值的---赋值雪花Id
                if (entityInfo.EntityColumnInfo.IsPrimarykey && entityInfo.EntityColumnInfo.PropertyInfo.PropertyType == typeof(long))
                {
                    var id = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue);
                    if (id == null || (long)id == 0)
                        entityInfo.SetValue(YitIdHelper.NextId());
                }
                if (entityInfo.PropertyName == "CreateTime")
                    entityInfo.SetValue(DateTime.Now);
                if (App.User != null)
                {
                    if (entityInfo.PropertyName == "TenantId")
                    {
                        var tenantId = ((dynamic)entityInfo.EntityValue).TenantId;
                        if (tenantId == null || tenantId == 0)
                            entityInfo.SetValue(App.User.FindFirst(ClaimConst.TenantId)?.Value);
                    }
                    if (entityInfo.PropertyName == "CreateUserId")
                    {
                        var createUserId = ((dynamic)entityInfo.EntityValue).CreateUserId;
                        if (createUserId == 0 || createUserId == null)
                            entityInfo.SetValue(App.User.FindFirst(ClaimConst.UserId)?.Value);
                    }

                    if (entityInfo.PropertyName == "CreateOrgId")
                    {
                        var createOrgId = ((dynamic)entityInfo.EntityValue).CreateOrgId;
                        if (createOrgId == 0 || createOrgId == null)
                            entityInfo.SetValue(App.User.FindFirst(ClaimConst.OrgId)?.Value);
                    }
                }
            }
            if (entityInfo.OperationType == DataFilterType.UpdateByObject)
            {
                if (entityInfo.PropertyName == "UpdateTime")
                    entityInfo.SetValue(DateTime.Now);
                if (entityInfo.PropertyName == "UpdateUserId")
                    entityInfo.SetValue(App.User?.FindFirst(ClaimConst.UserId)?.Value);
            }
        };

        // 超管排除其他过滤器
        if (App.User?.FindFirst(ClaimConst.AccountType)?.Value == ((int)AccountTypeEnum.SuperJR).ToString())
            return;

        // 配置假删除过滤器
        db.QueryFilter.AddTableFilter<IDeletedFilter>(u => u.IsDelete == false);

        // 配置租户过滤器
        var tenantId = App.User?.FindFirst(ClaimConst.TenantId)?.Value;
        if (!string.IsNullOrWhiteSpace(tenantId))
            db.QueryFilter.AddTableFilter<ITenantIdFilter>(u => u.TenantId == long.Parse(tenantId));

        // 配置用户机构（数据范围）过滤器
        SqlSugarFilter.SetOrgEntityFilter(db);

        // 配置自定义过滤器
        SqlSugarFilter.SetCustomEntityFilter(db);
    }

    /// <summary>
    /// 开启库表差异化日志
    /// </summary>
    /// <param name="db"></param>
    /// <param name="config"></param>
    private static void SetDbDiffLog(SqlSugarScopeProvider db, SqlSugarClient dbClient, DbConnectionConfig config)
    {
        if (!config.DbSettings.EnableDiffLog) return;

        //获取差异日志所在的数据库
        var logConfigId = SqlSugarConst.GetDataBaseConfigId(typeof(SysLogDiff));
        var dbDiffLogProvider = dbClient.GetConnectionScope(logConfigId);
        //db.DbMaintenance.
        //dbClient
        db.Aop.OnDiffLogEvent = async u =>
        {
            var logDiff = new SysLogDiff
            {
                // 操作后记录（字段描述、列名、值、表名、表描述）
                AfterData = JSON.Serialize(u.AfterData),
                // 操作前记录（字段描述、列名、值、表名、表描述）
                BeforeData = JSON.Serialize(u.BeforeData),
                // 传进来的对象
                BusinessData = JSON.Serialize(u.BusinessData),
                // 枚举（insert、update、delete）
                DiffType = u.DiffType.ToString(),
                Sql = UtilMethods.GetSqlString(config.DbType, u.Sql, u.Parameters),
                Parameters = JSON.Serialize(u.Parameters),
                Elapsed = u.Time == null ? 0 : (long)u.Time.Value.TotalMilliseconds
            };
            await dbDiffLogProvider.Insertable(logDiff).ExecuteCommandAsync();
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(DateTime.Now + $"\r\n*****差异日志开始*****\r\n{Environment.NewLine}{JSON.Serialize(logDiff)}{Environment.NewLine}*****差异日志结束*****\r\n");
        };
    }

    /// <summary>
    /// 初始化数据库
    /// </summary>
    /// <param name="db"></param>
    /// <param name="config"></param>
    private static void InitDatabase(SqlSugarScope db, DbConnectionConfig config)
    {
        SqlSugarScopeProvider dbProvider = db.GetConnectionScope(config.ConfigId);
        db.GetConnectionScope(config.ConfigId);

        // 初始化/创建数据库
        if (config.DbSettings.EnableInitDb)
        {
            if (config.DbType != SqlSugar.DbType.Oracle)
                dbProvider.DbMaintenance.CreateDatabase();
        }

        // 初始化表结构
        if (config.TableSettings.EnableInitTable)
        {
            var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false))
                .WhereIF(config.TableSettings.EnableIncreTable, u => u.IsDefined(typeof(IncreTableAttribute), false)).ToList();

            if (config.ConfigId == SqlSugarConst.MainConfigId) // 默认库（有系统表特性、没有日志表和租户表特性）
                entityTypes = entityTypes.Where(
                    u => u.GetCustomAttributes<SysTableAttribute>().Any()
                || (
                             !u.GetCustomAttributes<LogTableAttribute>().Any()
                             && !u.GetCustomAttributes<TenantAttribute>().Any()
                             && !u.GetCustomAttributes<BusinessAttribute>().Any()
                    )
                    ).ToList();
            else if (config.ConfigId == SqlSugarConst.LogConfigId) // 日志库
                entityTypes = entityTypes.Where(u => u.GetCustomAttributes<LogTableAttribute>().Any()).ToList();
            else if (config.ConfigId == SqlSugarConst.MESConfigId)
                entityTypes = entityTypes.Where(u => u.GetCustomAttributes<BusinessVSAttribute>().Any()).ToList();
            else if (config.ConfigId == SqlSugarConst.WMSConfigId)
                entityTypes = entityTypes.Where(u => u.GetCustomAttributes<BusinessWmsAttribute>().Any()).ToList();
            else
                entityTypes = entityTypes.Where(u => u.GetCustomAttribute<TenantAttribute>()?.configId.ToString() == config.ConfigId).ToList(); // 自定义的库



            foreach (var entityType in entityTypes)
            {
                //if (!entityType.Name.Contains("Log"))
                //    continue;

                try
                {
                    if (entityType.GetCustomAttribute<SplitTableAttribute>() == null)
                        dbProvider.CodeFirst.InitTables(entityType);
                    else
                        dbProvider.CodeFirst.SplitTables().InitTables(entityType);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
                if (config.DbType == SqlSugar.DbType.Oracle)
                {
                    var CodeFirst_BigStringFields = entityType.GetProperties()
                         .Where(r => r.GetCustomAttribute<SugarColumn>() != null && r.GetCustomAttribute<SugarColumn>().ColumnDataType == StaticConfig.CodeFirst_BigString)
                         .Select(r => r.Name).ToList();

                    if (CodeFirst_BigStringFields.Count > 0)
                    {
                        string entityName = entityType.Name;
                        var sugarTable = entityType.GetCustomAttribute<SugarTable>();
                        if (sugarTable != null && !sugarTable.TableName.IsNullOrEmpty())
                            entityName = sugarTable.TableName;

                        foreach (var fieldName in CodeFirst_BigStringFields)
                        {
                            try
                            {
                                string deleteColumn = $"ALTER TABLE {config.TableSettings.TablePrefix}{entityName}{config.TableSettings.TableSuffix} DROP COLUMN {fieldName}";
                                dbProvider.Ado.ExecuteCommand(deleteColumn);
                            }
                            catch (Exception ex) { }
                            try
                            {
                                string addTempColumn = $"ALTER TABLE {config.TableSettings.TablePrefix}{entityName}{config.TableSettings.TableSuffix} ADD ({fieldName}1 CLOB)";
                                dbProvider.Ado.ExecuteCommand(addTempColumn);
                            }
                            catch (Exception ex) { }
                            try
                            {
                                string updateToOrgColumn = $"ALTER TABLE {config.TableSettings.TablePrefix}{entityName}{config.TableSettings.TableSuffix} RENAME COLUMN {fieldName}1 TO {fieldName}";
                                dbProvider.Ado.ExecuteCommand(updateToOrgColumn);
                            }
                            catch (Exception ex)
                            {

                            }


                        }
                    }
                }
            }
        }

        // 初始化种子数据
        if (config.SeedSettings.EnableInitSeed)
        {
            var seedDataTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>))))
                .WhereIF(config.SeedSettings.EnableIncreSeed, u => u.IsDefined(typeof(IncreSeedAttribute), false)).ToList();

            foreach (var seedType in seedDataTypes)
            {
                var entityType = seedType.GetInterfaces().First().GetGenericArguments().First();
                if (config.ConfigId == SqlSugarConst.MainConfigId) // 默认库（有系统表特性、没有日志表和租户表特性）
                {
                    if (entityType.GetCustomAttribute<SysTableAttribute>() == null && (entityType.GetCustomAttribute<LogTableAttribute>() != null || entityType.GetCustomAttribute<TenantAttribute>() != null))
                        continue;
                }
                else if (config.ConfigId == SqlSugarConst.LogConfigId) // 日志库
                {
                    if (entityType.GetCustomAttribute<LogTableAttribute>() == null)
                        continue;
                }
                else if (config.ConfigId == SqlSugarConst.MESConfigId)
                {
                    if (entityType.GetCustomAttribute<BusinessVSAttribute>() == null)
                        continue;
                }
                else if (config.ConfigId == SqlSugarConst.WMSConfigId)
                {
                    if (entityType.GetCustomAttribute<BusinessWmsAttribute>() == null)
                        continue;
                }
                else
                {
                    var att = entityType.GetCustomAttribute<TenantAttribute>(); // 自定义的库
                    if (att == null || att.configId.ToString() != config.ConfigId) continue;
                }

                var instance = Activator.CreateInstance(seedType);
                var hasDataMethod = seedType.GetMethod("HasData");
                var seedData = ((IEnumerable)hasDataMethod?.Invoke(instance, null))?.Cast<object>();
                if (seedData == null) continue;

                var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(entityType);
                try
                {

                    if (entityInfo.Columns.Any(u => u.IsPrimarykey))
                    {
                        // 按主键进行批量增加和更新
                        var storage = dbProvider.StorageableByObject(seedData.ToList()).ToStorage();
                        storage.AsInsertable.ExecuteCommand();
                        storage.AsUpdateable.ExecuteCommand();
                    }
                    else
                    {
                        // 无主键则只进行插入
                        if (!dbProvider.Queryable(entityInfo.DbTableName, entityInfo.DbTableName).Any())
                            dbProvider.InsertableByObject(seedData.ToList()).ExecuteCommand();
                    }
                }
                catch (Exception ex)
                {

                }
            }
        }

        #region 数据库表映射为代码模型
        //if(config.ConfigId == SqlSugarConst.MESConfigId)
        //{
        //    db.DbFirst
        //    //类
        //    .SettingClassTemplate(old => { return old;/*修改old值替换*/ })
        //    //类构造函数
        //    .SettingConstructorTemplate(old => { return old;/*修改old值替换*/ })
        //     .SettingNamespaceTemplate(old => {
        //         return old + "\r\nusing SqlSugar;"; //追加引用SqlSugar
        //     })
        //    //属性备注
        //    .SettingPropertyDescriptionTemplate(old => { return old;/*修改old值替换*/})

        //    //属性:新重载 完全自定义用配置
        //    .SettingPropertyTemplate((columns, temp, type) => {

        //        var columnattribute = "\r\n           [SugarColumn({0})]";
        //        List<string> attributes = new List<string>();
        //        if (columns.IsPrimarykey)
        //            attributes.Add("IsPrimaryKey=true");
        //        if (columns.IsIdentity)
        //            attributes.Add("IsIdentity=true");
        //        if (attributes.Count == 0)
        //        {
        //            columnattribute = "";
        //        }
        //        return temp.Replace("{PropertyType}", type)
        //                    .Replace("{PropertyName}", columns.DbColumnName)
        //                    .Replace("{SugarColumn}", string.Format(columnattribute, string.Join(",", attributes)));
        //    })

        //   .CreateClassFile(config.DbSettings.DbTablesToModelsPath);
        //} 
        #endregion
    }

    /// <summary>
    /// 初始化租户业务数据库
    /// </summary>
    /// <param name="iTenant"></param>
    /// <param name="config"></param>
    public static void InitTenantDatabase(ITenant iTenant, DbConnectionConfig config)
    {
        SetDbConfig(config);

        iTenant.AddConnection(config);
        var db = iTenant.GetConnectionScope(config.ConfigId);
        db.DbMaintenance.CreateDatabase();

        // 获取所有系统表-初始化租户库表结构
        var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass &&
            u.IsDefined(typeof(SugarTable), false) && !u.IsDefined(typeof(SysTableAttribute), false) && !u.IsDefined(typeof(TenantAttribute), false)).ToList();
        if (!entityTypes.Any()) return;
        foreach (var entityType in entityTypes)
        {
            var splitTable = entityType.GetCustomAttribute<SplitTableAttribute>();
            if (splitTable == null)
                db.CodeFirst.InitTables(entityType);
            else
                db.CodeFirst.SplitTables().InitTables(entityType);
        }
    }

    public static ConditionalType GetConditionalType(string conditions)
    {
        ConditionalType condType = ConditionalType.Equal;
        //IsNullOrEmpty,
        //IsNot,
        //EqualNull,
        //InLike
        switch (conditions)
        {
            case "=":
                condType = ConditionalType.Equal;
                break;
            case "≠":
                condType = ConditionalType.NoEqual;
                break;
            case "like":
                condType = ConditionalType.Like;
                break;
            case "not like":
                condType = ConditionalType.NoLike;
                break;
            case "like left":
                condType = ConditionalType.LikeLeft; // xxxx%
                break;
            case "like right":
                condType = ConditionalType.LikeRight; // %xxxx
                break;
            case ">":
                condType = ConditionalType.GreaterThan;
                break;
            case ">=":
                condType = ConditionalType.GreaterThanOrEqual;
                break;
            case "<":
                condType = ConditionalType.LessThan;
                break;
            case "<=":
                condType = ConditionalType.LessThanOrEqual;
                break;
            case "in":
                condType = ConditionalType.In;
                break;
            case "not in":
                condType = ConditionalType.NotIn;
                break;
        }
        return condType;
    }
}

//扩展类型          
public class SqlSugarTypeProvider : DefaultDynamicLinqCustomTypeProvider
{
    public override HashSet<Type> GetCustomTypes()
    {
        var customTypes = base.GetCustomTypes();
        customTypes.Add(typeof(SqlFunc));//识别SqlFunc
        return customTypes;
    }
}
